const colorObject = {

    /**
     * hex 颜色转 rgb 颜色
     * @param hex hex 颜色
     * @constructor rgb 颜色
     */
    hexToRgb: (hex) => {
        hex = hex.replace("#", "");
        const hxs = hex.match(/../g);
        for (let i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16);
        return hxs;
    },

    /**
     * rgb 颜色转 hex 颜色
     * @param a r
     * @param b g
     * @param c b
     * @returns {string} hex
     */
    rgbToHex: (a, b, c) => {
        const hxs = [a.toString(16), b.toString(16), c.toString(16)];
        for (let i = 0; i < 3; i++) {
            if (hxs[i].length === 1) hxs[i] = "0" + hxs[i];
        }
        return "#" + hxs.join("");
    },

    /**
     * 颜色加深
     * @param color 颜色
     * @param level 加深程度
     * @returns {string} 颜色
     */
    darken: (color, level) => {
        const rgbC = colorObject.hexToRgb(color);
        for (let i = 0; i < 3; i++) rgbC[i] = Math.floor(rgbC[i] * (1 - level));
        return colorObject.rgbToHex(rgbC[0], rgbC[1], rgbC[2]);
    },

    /**
     * 颜色变淡
     * @param color 颜色
     * @param level 变淡程度
     * @returns {string} 颜色
     */
    lighten: (color, level) => {
        const rgbC = colorObject.hexToRgb(color);
        for (let i = 0; i < 3; i++) rgbC[i] = Math.floor((255 - rgbC[i]) * level + rgbC[i])
        return colorObject.rgbToHex(rgbC[0], rgbC[1], rgbC[2])
    }

};

export default colorObject;
